﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinRTDatabase;

namespace WinRTDbQuickStart
{
    public class MainViewModel : PropertyChangedCore
    {
        public string DatabaseName = "TestDb";
        private Database _database;


        public MainViewModel()
        {
            DeleteCommand = new SimpleCommand<Person>(OnDelete);
            AddCommand = new SimpleCommand<Person>(OnAdd, CanAdd);
            SaveCommand = new SimpleCommand<object>(OnSave, CanSave);
            Initialise();
        }

        public async void Initialise()
        {
            //await Database.DeleteDatabase(DatabaseName, StorageLocation.Local);
            var exists = await Database.DoesDatabaseExistsAsync(DatabaseName, StorageLocation.Local);
            if (!exists)
            {
                _database = await Database.CreateDatabaseAsync(DatabaseName, StorageLocation.Local);
                _database.CreateTable<Person>();
                var table = await _database.Table<Person>();
                await _database.SaveAsync();
                People = table;
            }
            else
            {
                _database = await Database.OpenDatabaseAsync(DatabaseName, true, StorageLocation.Local);
                People = await _database.Table<Person>();
            }
            
            SaveCommand.RaiseCanExecuteChanged();
            AddCommand.RaiseCanExecuteChanged();
            if (People.Count > 0)
            {
                SelectedPerson = People.First();
            }
            _database.PropertyChanged += new PropertyChangedEventHandler(_database_PropertyChanged);
        }

        void _database_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SaveCommand.RaiseCanExecuteChanged();
            AddCommand.RaiseCanExecuteChanged();
        }

        private Table<Person> people;

        public Table<Person> People
        {
            get { return people; }
            set
            {
                people = value; OnPropertyChanged("People");
                if (people.Count > 0)
                {
                    SelectedPerson = people.First<Person>();
                }
            }
        }

        private Person selectedPerson;

        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                selectedPerson = value;
                OnPropertyChanged("SelectedPerson");
            }
        }

        public SimpleCommand<Person> DeleteCommand { get; set; }

        public void OnDelete(Person parameter)
        {
            if (parameter != null)
            {
                People.Remove(parameter);
                if (People.Count > 0)
                {
                    SelectedPerson = People.First();
                }
            }
        }

        public SimpleCommand<Person> AddCommand { get; set; }

        public void OnAdd(Person parameter)
        {
            People.Add(new Person { PersonID = Guid.NewGuid(), FirstName=" ", LastName = " " });
            SelectedPerson = People.Last();
        }
        public bool CanAdd(object parameter)
        {
            return (_database != null && People != null);
        }

        public SimpleCommand<object> SaveCommand { get; set; }

        public void OnSave(object parameter)
        {
            if (_database != null)
            {
                _database.SaveAsync();
            }
        }
        public bool CanSave(object parameter)
        {
            return (_database != null && !_database.IsBusy && _database.IsDirty);
        }
    }
}
